package com.enjava;

import java.io.Serializable;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Transient;

import org.springframework.web.bind.annotation.PathVariable;


@ManagedBean(name="wizardBean")
@SessionScoped
public class WizardBean implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Curso curso = new Curso();
	Alumno alumno = new Alumno();
	Examen examen = new Examen();
	
	
	Curso cursoEdicion = new Curso();
	
	@ManagedProperty(value = "#{emf}")
	transient EMF emf; // Transient le dice al compiler que no es un atributo serializable
	
	public Curso getCursoEdicion() {
		return cursoEdicion;
	}

	public void setCursoEdicion(Curso cursoEdicion) {
		this.cursoEdicion = cursoEdicion;
	}

	public EMF getEmf() {
		return emf;
	}

	public void setEmf(EMF emf) {
		this.emf = emf;
	}

	public Examen getExamen() {
		return examen;
	}

	public void setExamen(Examen examen) {
		this.examen = examen;
	}

	public Alumno getAlumno() {
		return alumno;
	}

	public void setAlumno(Alumno alumno) {
		this.alumno = alumno;
	}

	public Curso getCurso() {
		return curso;
	}

	public void setCurso(Curso curso) {
		this.curso = curso;
	}

	public String next(){
	
		
		FacesContext context = FacesContext.getCurrentInstance(); 
		Map map = context.getExternalContext().getRequestParameterMap(); 
		int pasoNext= Integer.parseInt((String) map.get("nextPaso")); 
		
		switch (pasoNext) {
		case 1:
			return "wizardPaso2?faces-redirect=true";

		case 2:
			return "wizardPaso3?faces-redirect=true";

		case 3:
			return "wizardPaso4?faces-redirect=true";

		case 4:
			return "wizardPasoFinish?faces-redirect=true";

		case 5:
			System.out.println("persistir");
			break;
		default:
			break;
		}
		
		return null;
	}
	
	public String back(){
		
		FacesContext context = FacesContext.getCurrentInstance(); 
		Map map = context.getExternalContext().getRequestParameterMap(); 
		int pasoBack = Integer.parseInt((String) map.get("backPaso")); 
		
		System.out.println(pasoBack);
		switch (pasoBack) {
			case 2:
				return "wizardPaso1?faces-redirect=true";
			case 3:
				return "wizardPaso2?faces-redirect=true";
			case 4:
				return "wizardPaso3?faces-redirect=true";
			default:
			break;
		}
		
		return null;
	}
	
	public String finish(){
		System.out.println("guardar con emf");
		
//		alumno.curso = curso;
		curso.alumnos.add(alumno);
//		examen.curso = curso;
		curso.examenes.add(examen);

		EntityManager em = emf.getEM();
		
		EntityTransaction transaccion = em.getTransaction();
		transaccion.begin();
		em.persist(curso);
		transaccion.commit();
		
		return "wizardPasoFinish?faces-redirect=true" ; 
	}

	
	public String inicio(){
		curso = new Curso();
		alumno = new Alumno();
		examen = new Examen();
		
		return "wizardPaso1?faces-redirect=true";
	}

}
